package edu.ncu.controller;

import edu.ncu.entity.Friend;
import edu.ncu.entity.FriendAsk;
import edu.ncu.entity.User;
import edu.ncu.service.FriendAskService;
import edu.ncu.service.FriendService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/friendAsk")
public class FriendAskController {
    final FriendAskService friendAskService;
    final FriendService friendService;

    public FriendAskController(FriendAskService friendAskService, FriendService friendService) {
        this.friendAskService = friendAskService;
        this.friendService = friendService;
    }

    @RequestMapping("/findByReceiverId")
    public String findByReceiverId(ModelMap modelMap, HttpServletRequest request)
    {
        // List<User> senders = friendAskService.findByUserId(receiverId);
        User user = (User)  request.getSession().getAttribute("user") ;
        List<FriendAsk> asks = friendAskService.findFriendAsk(user.getUserId()) ;
        // System.out.println("请求者们："+senders);
        asks.forEach((f) -> {
            System.out.println(f);
        });
        modelMap.put("asks", asks);
        return "person/friendsRequests";
    }

    //处理好友请求
    // 登录用户作为好友的接收者
    @RequestMapping("/requestDeal")
    public String agree(@RequestParam("senderId") int senderId, @RequestParam("receiverId") int receiverId,
                        @RequestParam("result") int result)
    {
        System.out.println("接收到的参数：senderId:"+senderId+"  receiverId:"+receiverId+"  "+result);
        if(result == 1) {
            //更新请求的result为1，state为1，保存两条朋友记录
            friendAskService.update(1, receiverId,senderId);
            Friend friend1 = new Friend();
            friend1.setFriendId(senderId);
            friend1.setUserId(receiverId);
            friend1.setFriendTime(new Date());
            friendService.saveFriend(friend1);

            Friend friend2 = new Friend();
            friend2.setFriendId(receiverId);
            friend2.setUserId(senderId);
            friend2.setFriendTime(new Date());
            friendService.saveFriend(friend2);
        } else {
            friendAskService.update(0, receiverId,senderId);
        }
        return "redirect:/friendAsk/findByReceiverId?receiverId="+receiverId;
    }

    //发送加好友请求
    @RequestMapping("/saveFriendAsk")
    public String saveFriendAsk(
            @RequestParam("senderId") int senderId,
            @RequestParam("receiverId") int receiverId)
    {
        /**
         * 发送好友请求时，需要检测是否是添加了自己
         * 也需要检测是否为重复添加
         * 检查好友请求的接受者是否为自己的好友
         */
        System.out.println(senderId+"  "+receiverId);
        if (senderId != receiverId) {
            if (friendService.checkIsFriend(senderId,receiverId) == null) {
                FriendAsk friendAsk = new FriendAsk();
                friendAsk.setSenderId(senderId);
                friendAsk.setReceiverId(receiverId);
                friendAsk.setState(0);
                System.out.println("发送的好友请求："+friendAsk);
                friendAskService.saveFriendAsk(friendAsk);
            }
        }
        return "redirect:/friendAsk/findByReceiverId?receiverId="+senderId;
    }
}
